/**
 * 文章
 */
import api from '../../tools/api'
import {SUCCESS_STATUS} from '../../config/status'
import promise from '../../tools/promise'
import {
  ARTICLE_LIST,
  ARTICLE_TREE,
  ARTICLE_COVER,
  ARTICLE_INFO,
  ARTICLE_CLASS_INFO,
  ARTICLE_CLASS_SAVE_DATA
} from '../mutation-types'

const state = {
  pageData: {
    total: 1,
    per_page: 15,
    current_page: 1,
    last_page: 1
  },
  articleList: [],
  articleInfo: {},
  classTree: [],
  classInfo: {},
  saveData: {},
  cover: {}
}

const mutations = {
  [ARTICLE_LIST] (state, payload) {
    state.articleList = payload.data
    state.pageData.total = Number(payload.total)
    state.pageData.per_page = Number(payload.per_page)
    state.pageData.current_page = Number(payload.current_page)
    state.pageData.last_page = Number(payload.last_page)
  },
  [ARTICLE_TREE] (state, payload) {
    state.classTree = payload
  },
  [ARTICLE_INFO] (state, payload) {
    state.articleInfo = payload
  },
  [ARTICLE_CLASS_INFO] (state, payload) {
    state.classInfo = payload
  },
  [ARTICLE_CLASS_SAVE_DATA] (state, payload) {
    state.saveData = payload
  },
  [ARTICLE_COVER] (state, data) {
    state.cover = data
  }
}

const actions = {
  // 获取文章列表
  async articleList ({commit}, payload) {
    let listRes = await api('serverarticle/article/ApiArticle/articleList', payload)
    commit(ARTICLE_LIST, listRes.res)
  },
  // 添加文章
  async articleAdd ({commit}, payload) {
    let addRes = await api('serverarticle/article/ApiArticle/add', payload)
    return promise(addRes)
  },
  // 编辑文章
  async articleEdit ({commit}, payload) {
    let editRes = await api('serverarticle/article/ApiArticle/edit', payload)
    return promise(editRes)
  },
  // 审核通过文章
  async articlePass ({commit}, payload) {
    let passRes = await api('serverarticle/article/ApiArticle/audit', payload)
    return promise(passRes)
  },
  // 审核失败文章
  async articleFailed ({commit}, payload) {
    let stopRes = await api('serverarticle/article/ApiArticle/auditFailure', payload)
    return promise(stopRes)
  },
  // 删除文章
  async articleDel ({commit}, payload) {
    let delRes = await api('serverarticle/article/ApiArticle/delete', payload)
    return promise(delRes)
  },
  // 获取文章详情
  async articleInfo ({commit}, payload) {
    let infoRes = await api('serverarticle/article/ApiArticle/articleInfo', payload)
    commit(ARTICLE_INFO, infoRes.res)
  },
  // 获取文章分类树
  async classTree ({commit}, payload) {
    let treeRes = await api('serverarticle/article/ApiArticleClassify/classifyList', payload)
    if (SUCCESS_STATUS === treeRes.status) {
      commit(ARTICLE_TREE, treeRes.res)
    }
  },
  // 启用文章分类
  async classStart ({commit}, payload) {
    let startRes = await api('serverarticle/article/ApiArticleClassify/classifyEnable', payload)
    return promise(startRes)
  },
  // 禁用文章分类
  async classStop ({commit}, payload) {
    let stopRes = await api('serverarticle/article/ApiArticleClassify/classifyDisable', payload)
    return promise(stopRes)
  },
  // 删除文章分类
  async classDel ({commit}, payload) {
    let delRes = await api('serverarticle/article/ApiArticleClassify/classifyDelete', payload)
    return promise(delRes)
  },
  // 添加文章分类
  async classAdd ({commit}, payload) {
    let addRes = await api('serverarticle/article/ApiArticleClassify/add', payload)
    return promise(addRes)
  },
  // 编辑文章分类
  async classEdit ({commit}, payload) {
    let editRes = await api('serverarticle/article/ApiArticleClassify/edit', payload)
    return promise(editRes)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
